export NAME='press.js'

export P_NAME=\"$NAME\"
export V=$((`svn info | grep Revision | sed 's/Revision: //'`+1))
export P_VERSION=\"'1.0.'`printf "%i.%i" $(($V/10)) $(($V%10))`\"

echo
echo Building $NAME version $P_VERSION
echo

#svn up

export OPTS="-I./temp -DP_NAME=$P_NAME -DP_VERSION=$P_VERSION"

mkdir temp; mkdir temp/target

cd src

for FILE in `find * | sed 's/.*\.svn.*//g; s/^[^\.]*$//g;'`
do
  echo Preprocessing $FILE
  
  cat $FILE  \
  | sed 's/^\s*\/\/#/#/'  \
  > ../temp/$FILE;
  
done
echo
cd ..

for FILE in `ls temp/target`
do
  echo Processing $FILE
  
  cat temp/target/$FILE  \
  | cpp $OPTS  \
  | sed 's/^[#:].*// ; /^$/d'  \
  > build/`basename $FILE`;
  
done

export SFX=build/unpress.sfx.min.js

echo 'new Function("j",'>$SFX

cat build/unpress.sfx.js | jsmin | sed 's/InStream/_a/g ; s/DecompressDictionary/_b/g ; s/ValSizeInBits/_c/g ; s/revhashtable/_d/g ; s/instream/_e/g ; s/nextcode/_f/g ; s/encodedString/_g/g; s/ReadBit/_h/g; s/numBits/_i/g; s/output/_j/g; s/decompress/_k/g; s/LookupIndex/_l/g; s/numBits/_m/g; s/decodeChunk/_n/g; s/decode/_o/g; s/offset/_p/g; s/toArray/_q/g; s/Read/_r/g; s/bytestream/_s/g; s/encoded/_t/g; s/bitcount/_u/g; s/dict/_v/g; s/Size/_w/g; s/entry/_x/g; s/idx/_z/g; s/pressJs/_z/g; s/toNumber/_A/g; s/Insert/_B/g;           s/function/#f/g; s/return /#r/g; s/this/#t/g;  s/String\.fromCharCode/#S/g;     /^$/d ' | awk -v RS="\n" -v ORS="@" {print}  | sed  's/\\/\\\\/g; s/@/\\n/g;  s/$/"/; s/^/"/;' >> $SFX;

echo ".replace(/(#f)|(#t)|(#r)|(#S)/g,function(x,f,t,r,s){return f?'function':t?'this':r?'return ':s?'String.fromCharCode':''}))" >> $SFX;

echo `cat $SFX` > $SFX ;

rm -r temp

echo

